﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calligraphy.CalligraphyData
{
    partial class T_RubbingPhoto
    {
        Uri _photo250Uri;
        public Uri Photo250
        {
            get
            {
                if (_photo250Uri == null)
                {
                    LoadSmallPhotoEntities();
                }
                return _photo250Uri;
            }
        }

        string _searchName;

        public string SearchName
        {
            get 
            {
                string subName = string.Empty;
                if (T_Rubbing != null)
                    subName += T_Rubbing.Name;
                return subName; 
            }
            set { _searchName = value; }
        }
        
        string _searchResultSubtitle;

        public string SearchResultSubtitle
        {
            get 
            {
                string subtitle = string.Empty;
                if (T_Rubbing != null && T_Rubbing.T_StelePaper != null &&
                    T_Rubbing.T_StelePaper.T_Author != null &&
                    T_Rubbing.T_StelePaper.T_Dynast != null &&
                    T_Rubbing.T_StelePaper.T_Style != null)
                    subtitle = T_Rubbing.T_StelePaper.T_Author.Name.Trim() + " " + 
                        T_Rubbing.T_StelePaper.T_Dynast.Name.Trim() + " " +
                        T_Rubbing.T_StelePaper.T_Style.Name.Trim();
                 
                return subtitle;

            }
            set { _searchResultSubtitle = value; }
        }

        Uri _photoUri;
        public Uri Photo
        {
            get
            {
                if (_photoUri == null && T_Photo != null)
                {
                    LoadPhotoUri();
                }
                return _photoUri;
            }
        }

        private async void LoadPhotoUri()
        {
            _photoUri =
                    await PhotoLoader.GetImageUriAsync(this.T_Photo);
            if (_photoUri == null)
                return;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("Photo"));
            }
        }

        private void LoadSmallPhotoEntities()
        {
            try
            {
                if (T_Photo == null)
                    return;
                if (T_Photo.T_Photo1.Count > 0)
                    return;
                var q = from sp in DataServiceContextHelper.DataContext.T_Photo where sp.RawPhotoID == T_Photo.ID && (sp.IsDeleted == null || sp.IsDeleted == false) select sp;
                T_Photo.T_Photo1.LoadAsync(q);
                T_Photo.T_Photo1.LoadCompleted += T_Photo1_LoadCompleted;
            }catch(Exception)
            {
            }
        }
        void T_Photo1_LoadCompleted(object sender, System.Data.Services.Client.LoadCompletedEventArgs e)
        {
            LoadPhoto250Uri();
        }

        private async void LoadPhoto250Uri()
        {
            T_Photo smallphotoEntity = PhotoLoader.GetSmallPhoto(T_Photo, PhotoLoader.SmallPhotoSizes250);
            if (smallphotoEntity != null)
            {
                _photo250Uri =
                    await PhotoLoader.GetImageUriAsync(smallphotoEntity);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("Photo250"));
                }
            }
        }
    }
}
